package com.whl.sm.controller.system;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whl.sm.common.result.Result;
import com.whl.sm.common.util.UploadUtils;
import com.whl.sm.controller.BaseController;
import com.whl.sm.model.system.OrgUserInfo;
import com.whl.sm.service.system.OrgUserInfoService;
import com.whl.sm.service.system.OrgUserService;
import com.whl.sm.vo.OrgUserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuhaolun
 * @since 2021-09-10
 */
@Slf4j
@RestController
@RequestMapping("/org-user-info")
public class OrgUserInfoController extends BaseController {

    @Autowired
    OrgUserInfoService orgUserInfoService;

    @Value("${ROOT_PATH}")
    private String rootPath;


    @RequestMapping("/get/{userId}")
    public Result getUserInfo(@PathVariable("userId") String userId){
        OrgUserInfo orgUserInfo = orgUserInfoService.getOne(new QueryWrapper<OrgUserInfo>()
                .eq("user_id",userId
        ));
        return Result.ok(orgUserInfo);
    }

    @RequestMapping("/getUserByName")
    public Result getUserByName(HttpServletRequest request){
        Page<Object> userList = new Page<>();
        String likeName = request.getParameter("likeName");
        if (likeName.equals("undefined") && !likeName.equals("")){
            return Result.ok(userList);
        }
        QueryWrapper queryWrapper = getLikeParam("user_name");
        userList = orgUserInfoService.page(getPage(),queryWrapper);
        return Result.ok(userList);
    }

    @RequestMapping("/currentUserInfo")
    public Result getByCurrentUSer(){
        return Result.ok(orgUserInfoService.getByCurrentUser());
    }

    @RequestMapping("/update")
    public Result updateInfo(@RequestBody OrgUserInfo orgUserInfo){
        return orgUserInfoService.updateInfo(orgUserInfo);
    }

    @RequestMapping("/wxUpdate")
    public Result wxUpdateInfo(OrgUserInfo orgUserInfo){
        return orgUserInfoService.updateInfo(orgUserInfo);
    }


    @RequestMapping("/insert")
    public Result insertUserInfo(@RequestBody OrgUserInfo orgUserInfo){
        boolean result =  orgUserInfoService.save(orgUserInfo);
        if (!result){
            return Result.fail();
        }
        return Result.ok();
    }

    @RequestMapping("/uploadAvatar")
    public Result upload(@RequestParam MultipartFile avatar, HttpServletRequest request){
        String path = "/user/avatar";
        String resultPath = "";
        try {
            List<Map<String,Object>> fileList = UploadUtils.saveFile(request,path,this.rootPath,"avatar");
            if (fileList.size()>0){
                for (Map<String,Object> map : fileList){
                    resultPath = (String) map.get("filePath");
                }
            }
            OrgUserInfo targetUser = orgUserInfoService.getByCurrentUser();
            targetUser.setAvatar(resultPath);
            Result updateRt = orgUserInfoService.updateInfo(targetUser);
            if (!updateRt.isOk()){
                return updateRt;
            }
        }catch (Exception e){
            log.error("资产管理----->单元管理:上传单元缩略图失败!");
            return Result.fail();
        }
        return Result.ok(resultPath);
    }


}
